package org.hzero.order.api.controller.v1;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.order.app.service.LineService;
import org.hzero.order.config.SwaggerTags;
import org.hzero.order.domain.entity.Line;
import org.hzero.order.domain.repository.LineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;

/**
 * @author xiuiqng.Yang-Estelle-양수청
 * @date 2019/8/7 19:37
 */

/**
 * API接口
 */
@Api(tags = SwaggerTags.LINE)
@RestController("lineController.v1")
@RequestMapping("/v1/{organizationId}/lines")
public class LineController extends BaseController {

    @Autowired
    private LineService lineService;
    @Autowired
    private LineRepository lineRepository;

    @ApiOperation(value = "新增订单行信息")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping
    public ResponseEntity<Line> createOrderLine(@RequestBody Line line){
        // 简单数据校验
        this.validObject(line);
        return Results.success(lineService.insertLine(line));
    }

    @ApiOperation(value = "修改订单行信息")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PutMapping
    public ResponseEntity<Line> updateOrder(@RequestBody Line line){
        // 简单数据校验
        this.validObject(line);
        Line line1 = lineRepository.selectByPrimaryKey(line);
        if (line!=null){
            lineService.updateLine(line);
        }
        return Results.success(line);
    }

    @ApiOperation(value = "提供删除订单行接口（页面一般为禁用）")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @DeleteMapping
    public ResponseEntity<Line> deleteOrderHeader(@RequestBody Line line){
        // 简单数据校验
        this.validObject(line);
        return Results.success(lineService.deleteLine(line));
    }

    @ApiOperation(value = "根据订单编号查询订行")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping
    public ResponseEntity<Page<Line>> queryHeaderList(
            Long soHeaderId,
            Integer lineNumber,
            PageRequest pageRequest){
        Line line = new Line();
        line.setSoHeaderId(soHeaderId);
        line.setLineNumber(lineNumber);
        return Results.success(lineRepository.pageLine(line, pageRequest));
    }
}
